package net.homeftp.heimo.weddingwidget.ui.imgdialog;

import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ImageListAdapter extends BaseAdapter  {
	private static final String TAG = "ImageListAdapter";
	private Context ctx;
	private int array_id;
	private TypedArray imgs= null;
	
	public ImageListAdapter(Context context, int array_id) {
		super();
		this.ctx = context;
		this.array_id = array_id;
		imgs = ctx.getResources().obtainTypedArray(array_id);
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		int img_id = getImageIdByPos(position);
		if(convertView!=null){
			if(convertView instanceof ImageView){
				((ImageView)convertView).setImageResource(img_id);				
			}
		}else{
			ImageView im = new ImageView(ctx);
			convertView = im;
			im.setImageResource(img_id);
		}
		return convertView;
	}

	@Override
	public int getCount() {
		return imgs.length();
	}
	
	public int getImageRefByPos(int pos){
		return getImageIdByPos(pos);
	}
	
	private int getImageIdByPos(int pos){
		return imgs.getResourceId(pos, -1);
	}
	
	@Override
	public Object getItem(int position) {
		return getImageIdByPos(position);
	}
	
	@Override
	public long getItemId(int position) {
		return position;
	}
}
